// const MD5 = require('../utils/MD5')

// import {MD5} from '../../utils/MD5.js';
//本地测试
// const baseUrl = 'http://localhost:8081';//基础域名
// const appId = 53;
// const appKey="76cc05cd-cbea-476e-b1ee-f16ddd759158"
// // 服务器端测试
// const baseUrl = 'http://119.91.204.198:8081';//基础域名
// const appId = 72;
// const appKey = 'd545ff5e-f0ef-432a-9285-17d3ef5d5293';
// 本地自己的后端
const baseUrl = 'http://localhost:8080';



/**
 * post 请求
 * @param {*} url 
 * @param {*} data 
 */
function post(url, data,others) {
  const timestamp = Date.now();
  // const token = generateToken(appId, appKey, timestamp);
  // const lastUrl = `${baseUrl}${url}?appId=${appId}&timeStamp=${timestamp}&token=${token}`;
  const token = wx.getStorageSync('token') || " ";
  const lastUrl = `${baseUrl}${url}`;
  const content_type = others || 'application/json';
  const postdata = data ||""
  console.log(data);
  console.log(lastUrl);
  console.log(content_type);

  return new Promise((resolve, reject) => {
    wx.request({
      url: lastUrl,
      method: 'POST',
      header: {
       'content-type': content_type,
       'Bearer':token
     },
      data: data,
      success: function(res) {
        console.log(res);
        if (res.data.code == 200) {
          console.log('post请求成功');
          resolve(res);
        } else {
          console.log('post请求发送成功，结果出错');
          
          resolve(res);
        }
      },
      fail: function(err) {
        console.log('post请求发送失败');
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: 'none',
          duration: 2000
        });
        reject(err);
      }
    });
  })
}

function get(url,data,others){
  const timestamp = Date.now();
  // const token = generateToken(appId, appKey, timestamp);
  // const lastUrl = `${baseUrl}${url}?appId=${appId}&timeStamp=${timestamp}&token=${token}`;
  const token = wx.getStorageSync('token') || " ";
  const lastUrl = `${baseUrl}${url}`;
  const content_type = others || 'application/json';
  console.log(data);
  console.log(lastUrl);
  console.log(content_type);

  return new Promise((resolve, reject) => {
    wx.request({
      url: lastUrl,
      method: 'GET',
      header: {'content-type': content_type},
      data: data,
      success: function(res){//请求成功
        console.log(res);
        if(res.data.code == 200){
          console.log('GET请求成功');
          resolve(res);
        }
        else{
          console.log('GET请求发送成功，结果出错');
          // reject(res);
          resolve(res);
        }
      },
      fail: function(err){//请求失败
        console.log('GET请求发送失败');
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: 'none',
          duration: 2000
        })
        reject(err);
      }
    })
  })
}

function uploadFile(data,filePath){
  const timestamp = Date.now();
  const token = generateToken(appId, appKey, timestamp);
  const url = '/file/api/upload';
  const lastUrl = `${baseUrl}${url}?appId=${appId}&timeStamp=${timestamp}&token=${token}`;
  const content_type = 'multipart/form-data';
  console.log(data);
  console.log(lastUrl);
  console.log(content_type);

  return new Promise((resolve, reject) => {
    wx.uploadFile({
      filePath: filePath,
      name: 'stk_file',
      url: lastUrl,
      formData:data,
      header: {'content-type': content_type},
      success: function(res){
        console.log(res);
        // console.log('this is res.data');
        // console.log(res.data);
        // console.log(typeof(res.data));
        // console.log(res.data.code);
        console.log("______________________________________________________")
        resolve(res);
        // 返回结果是字符串，所以无法采用该方法进行判断是否上传成功
        // if (res.data.code == 200) {
        //   console.log('文件上传请求成功');
        //   resolve(res);
        // } else {
        //   console.log('文件上传请求发送成功，结果出错');
        //   wx.showToast({
        //     title: '未知错误',
        //     icon: 'none',
        //     duration: 2000
        //   });
        //   reject(res);
        // }
      },
      fail: function(err) {
        console.log('文件上传请求发送失败');
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: 'none',
          duration: 2000
        });
        reject(err);
      }
    })
    wx.request({
      success: function(res) {
        console.log(res);
        if (res.data.code == 200) {
          console.log('post请求成功');
          resolve(res);
        } else {
          console.log('post请求发送成功，结果出错');
          wx.showToast({
            title: '未知错误',
            icon: 'none',
            duration: 2000
          });
          reject(res);
        }
      },
      fail: function(err) {
        console.log('post请求发送失败');
        console.log(err);
        wx.showToast({
          title: '系统错误，请稍后重试',
          icon: 'none',
          duration: 2000
        });
        reject(err);
      }
    });
  })
}

// /**
//  * 使用MD5产生token
//  * @param {*} appId 
//  * @param {*} appKey 
//  * @param {*时间戳} timestamp 
//  */
// function generateToken(appId, appKey, timestamp){
//     const param = "ApiRequest{appId="+appId+",appKey="+appKey+",timeStamp="+timestamp+"}";
//     let tokenStr = MD5.instance.hex_md5(param);
//     console.log('token: ' + tokenStr);
//     return tokenStr;
// }
// 导出函数，供其他文件调用
module.exports = {
  post: post,
  get: get,
  uploadFile:uploadFile
}